﻿package com.xx.xx;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;



/**
 * Created by yx on 2018/6/25.
 */
public class TextViewImgUtil {
    /**
     * 设置textView顶部图片
     * @param v 要设置的TextView
     * @param id_drawable 还要设置的图片
     */
    public static void setDrawableTop(Context mContext, TextView v, int id_drawable){
        Drawable topDrawable = ContextCompat.getDrawable(mContext, id_drawable);
        topDrawable.setBounds(0, 0, topDrawable.getMinimumWidth(), topDrawable.getMinimumHeight());
        //void android.widget.TextView.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
        v.setCompoundDrawables(null, topDrawable, null, null);
    }
    /**
     * 设置textView底部图片
     * @param v 要设置的TextView
     * @param id_drawable 还要设置的图片
     */
    public static void setDrawableBottom(Context mContext, TextView v, int id_drawable){
        Drawable drawable = ContextCompat.getDrawable(mContext, id_drawable);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        //void android.widget.TextView.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
        v.setCompoundDrawables(null, null, null, drawable);
    }
    /**
     * 设置textView右边图片
     * @param v 要设置的TextView
     * @param id_drawable 还要设置的图片
     */
    public static void setDrawableRight(Context mContext, TextView v, int id_drawable){
        Drawable rightDrawable = ContextCompat.getDrawable(mContext, id_drawable);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        //void android.widget.TextView.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
        v.setCompoundDrawables(null, null, rightDrawable, null);
    }

    /**
     * 设置textView左边图片
     * @param v 要设置的TextView
     * @param id_drawable 还要设置的图片
     */
    public static void setDrawableLeft(Context mContext, TextView v, int id_drawable){
        Drawable rightDrawable = ContextCompat.getDrawable(mContext, id_drawable);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        //void android.widget.TextView.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
        v.setCompoundDrawables(rightDrawable, null, null, null);
    }

    /**
     * 设置textView顶部和底部图片
     * @param v 要设置的TextView
     * @param id_drawableTop id_drawableBottom 还要设置的图片
     */
    public static void setDrawableTopBottom(Context mContext, TextView v, int id_drawableTop, int id_drawableBottom){
        Drawable topDrawable = ContextCompat.getDrawable(mContext, id_drawableTop);
        topDrawable.setBounds(0, 0, topDrawable.getMinimumWidth(), topDrawable.getMinimumHeight());
        Drawable bottomDrawable = ContextCompat.getDrawable(mContext, id_drawableBottom);
        bottomDrawable.setBounds(0, 0, bottomDrawable.getMinimumWidth(), bottomDrawable.getMinimumHeight());
        //void android.widget.TextView.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
        v.setCompoundDrawables(null, topDrawable, null, bottomDrawable);
    }

}
